package com.njtoyo.taxi.admin.service.business.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njtoyo.taxi.admin.cache.PrimaryCachePutter;
import com.njtoyo.taxi.admin.mapper.master.entity.CallEndSend;
import com.njtoyo.taxi.admin.mapper.master.entity.RelationNumber;
import com.njtoyo.taxi.admin.mapper.master.mapper.CallEndSendMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.RelationNumberMapper;
import com.njtoyo.taxi.admin.rest.presenter.business.RelationNumberPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.operation_rule.relation_number.RelationNumberAddUpdateWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.operation_rule.relation_number.RelationNumberBindWrapper;
import com.njtoyo.taxi.admin.service.business.RelationNumberService;
import com.njtoyo.taxi.admin.utils.private_number.PrivateBindResult;
import com.njtoyo.taxi.admin.utils.private_number.PrivateNumberClient;
import com.taxi.entity.common.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class RelationNumberServiceImpl extends ServiceImpl<RelationNumberMapper, RelationNumber> implements RelationNumberService {
    @Autowired
    private CallEndSendMapper callEndSendMapper;

    @Override
    public RestResult page(Integer current, Integer size, String driverRegionId, String mobileNumber) {
        Page<RelationNumberPresenter> page = new Page<>(current, size);
        IPage<RelationNumberPresenter> pages = baseMapper.getPage(page, driverRegionId, mobileNumber);
        for (RelationNumberPresenter relationNumber : pages.getRecords()) {
            Integer useCount = callEndSendMapper.selectCount(Wrappers.<CallEndSend>lambdaQuery()
                    .eq(CallEndSend::getTelX, relationNumber.getMobileNumber())
                    .between(CallEndSend::getCalltime, DateUtil.format(new Date(), "yyyy-MM-dd 00:00:00"),
                            DateUtil.format(new Date(), "yyyy-MM-dd 23:59:59")));
            relationNumber.setUsedCount(useCount);
        }
        return RestResult.success(pages);
    }

    @Override
    public RestResult switchIsActive(Long id, Boolean isActive) {
        RelationNumber relationNumber = new RelationNumber();
        relationNumber.setIsActive(isActive);
        relationNumber.setId(id);
        baseMapper.updateById(relationNumber);
        return RestResult.success();
    }

    @Override
    public RestResult add(RelationNumberAddUpdateWrapper wrapper) {
        RelationNumber relationNumber = new RelationNumber();
        relationNumber.setMobileNumber(wrapper.getMobileNumber());
        relationNumber.setIsActive(false);
        relationNumber.setAreaCode(wrapper.getDriverRegionId());
        baseMapper.insert(relationNumber);
        return RestResult.success();
    }

    @Override
    public RestResult update(RelationNumberAddUpdateWrapper wrapper) {
        RelationNumber relationNumber = new RelationNumber();
        relationNumber.setId(wrapper.getId());
        relationNumber.setMobileNumber(wrapper.getMobileNumber());
        relationNumber.setAreaCode(wrapper.getDriverRegionId());
        baseMapper.updateById(relationNumber);
        return RestResult.success();
    }

    @Override
    public RestResult bind(RelationNumberBindWrapper wrapper) {
        PrivateBindResult privateBindResult = PrivateNumberClient.bindMobileNumber(wrapper.getMobileNumber(), wrapper.getCallingNumber(), wrapper.getCalledNumber(),
                "300");
        if (!privateBindResult.isSuccess()) {
            return RestResult.failed("绑定失败");
        }
        return RestResult.success();
    }
}
